
#include "ScoreManager.h"
#include "LevelManager.h"
#include "SB.h"
#include "defs.h"
#include "TimeLineEffect.h"
#include "ScaleEffect.h"
#include "MoveEffect.h"
#include "FadeEffect.h"
#include "hMath.h"
#include "hRandom.h"
#include "ParticleManager.h"
#include <cmath>

ScoreManager::~ScoreManager()
{
    if (m_oRainbowEdge)
    {
        m_oRainbowEdge->~ParticleSystem();
    }
    if (m_oFullChain)
    {
        m_oFullChain->~ParticleSystem();
    }
    if (m_oAddChainParticles)
    {
        m_oAddChainParticles->~ParticleSystem();
    }
    if (m_oRainbowCurve)
    {
        delete m_oRainbowCurve;
    }
}

void ScoreManager::init()
{
    m_bScoreVisible = true;
    
    // success texts
    m_oaSuccessTexts[0] = new RenderableEntity("awesometext", Vector2(320.0f, 480.0f));
    m_oaSuccessTexts[0]->setCentered();
    m_oaSuccessTexts[0]->setScale(Vector2(3.0f, 3.0f));
    m_oaSuccessTexts[1] = new RenderableEntity("cooltext", Vector2(320.0f, 480.0f));
    m_oaSuccessTexts[1]->setCentered();
    m_oaSuccessTexts[1]->setScale(Vector2(3.0f, 3.0f));
    
    // chain text
    m_oChainText = new RenderableEntity("chaintext", Vector2(560.0f, 320.0f));
    m_oChainText->setCentered();
    m_oChainText->setScale(Vector2(1.0f, 1.0f));
    
	//Baby score
    m_eBabiesIcon = new RenderableEntity("icon_diaper", Vector2(160, 890));
    m_eBabiesIcon->setCentered();
    m_eBabiesIcon->setScale(Vector2(0.7f, 0.7f));
    
    m_tBabiesText = new TextEntity("0", FONT_NAME, 48, Vector2(200, 890), 0, 1);
    m_tBabiesText->setCenter(Vector2(0.0f, 0.5f));
    m_tBabiesText->setColor(COLOR_WHITE);

	//Coins score
    m_eCoinsIcon = new RenderableEntity("icon_babycoin", Vector2(580, 890));
    m_eCoinsIcon->setCentered();
    m_eCoinsIcon->setScale(Vector2(0.7f, 0.7f));

    m_tCoinsText = new TextEntity("0", FONT_NAME, 48, Vector2(540, 890), 2, 1);
    m_tCoinsText->setCenter(Vector2(1.0f, 0.5f));
    m_tCoinsText->setColor(COLOR_WHITE);
	
	m_eCoinsAnim = new TimeLineEffect(false, false);
	m_eCoinsAnim->addEffect(new ScaleEffect(m_tCoinsText, 0.1, false, false, Vector2(1.3, 1.3)), 0);
	m_eCoinsAnim->addEffect(new ScaleEffect(m_tCoinsText, 0.1, false, false, Vector2(1.0, 1.0)), 0.2);
	
	//Effect to babies counter
	m_eBabyParticle = new RenderableEntity("icon_diaper", Vector2(10, 850));
	m_eBabyParticle->setScale(Vector2(0.2, 0.2));
	m_eBabyParticle->setAlpha(0);
	
	m_eBabyParticleMove = new TimeLineEffect(false, false);
	
	//Effect to coins counter
	m_eNumerito = new TextEntity("000", FONT_NAME, 48, Vector2(0.0f, 0.0f), 1, 1);
    m_eNumerito->setCenter(Vector2(0.5, 0.5));
    m_eNumerito->setAlpha(0);
	
	m_eNumeritoAnim = new TimeLineEffect(false, false);
	m_eNumeritoAnim->addEffect(new FadeEffect(m_eNumerito, 0.10, false, false, 0, 1), 0);
	m_eNumeritoAnim->addEffect(new MoveEffect(m_eNumerito, 0.40, false, false, Vector2(0, 150), true), 0);
	m_eNumeritoAnim->addEffect(new FadeEffect(m_eNumerito, 0.10, false, false, 1, 0), 0.30);

	//Chain score multiplier and its effects
    m_tChain = new TextEntity("x0", FONT_NAME, 55, Vector2(560, 250), 1, 1);
    m_tChain->setCentered();
	
    m_eChainScale = new TimeLineEffect(false, false);
	m_eChainScaleBig = new ScaleEffect(m_tChain, 0.05, false, false, Vector2(1.3, 1.3));
	m_eChainScaleSmall = new ScaleEffect(m_tChain, 0.05, false, false, Vector2(1.0, 1.0));
	m_eChainScale->addEffect(m_eChainScaleBig, 0);
	m_eChainScale->addEffect(m_eChainScaleSmall, 0.05);
    
    // rainbow
    m_eChainHolder = new RenderableEntity("chainHolder", Vector2(560, 240));
    m_eChainHolder->setCentered();
    
    m_eRainbow = new RenderableEntity("rainbow", Vector2(640, 230));
    m_eRainbow->setScale(Vector2(1.25f, 1.19f));
    m_eRainbow->setColor(Vector4(1.0f, 1.0f, 1.0f, 0.5f));
    m_eRainbow->setCenter(Vector2(1.0f, 0.5f));

    m_oFullChain = ParticleManager::instance().addParticles( "rainbowFullChain", Vector3(560.0f, 240.0f, 0.0f));
    m_oFullChain->m_bRender = false;
    m_oFullChain->m_bStopSpawning = true;
    m_oRainbowEdge = ParticleManager::instance().addParticles( "rainbowEdge", Vector3(0.0f, 0.0f, 0.0f));
    m_oRainbowCurve = new BezierCurve(Vector2(0,250), Vector2(210,400), Vector2(580,360), Vector2(640,100));
    
    m_oAddChainParticles = ParticleManager::instance().addParticles( "addChain", Vector3(560.0f, 240.0f, 0.0f));
    m_oAddChainParticles->m_bRender = false;
    m_oAddChainParticles->m_bStopSpawning = true;
}

void ScoreManager::startGame()
{
    m_oFullChain->m_bRender = false;
    m_oFullChain->m_bStopSpawning = true;
    
    //Reset scores
    m_iCoinsToShow = 0;
    m_iCoins = 0;
    
    setBabies(0);
 
    char tiChar[5];
    sprintf(tiChar, "%d", 0);
    m_tCoinsText->setText( tiChar );
    
    //Reset rainbow
    m_fBarProgress = 0.0f;
    m_fBarProgressTarget = 0.0f;
}

void ScoreManager::renderSuccessTexts()
{
    if (m_fTimeSinceLastSuccess < m_fSuccesTextTime * 0.3f)
    {
        float fColor = m_fTimeSinceLastSuccess / (m_fSuccesTextTime * 0.3f);
        m_oaSuccessTexts[m_iCurrentText]->setColor(Vector4(fColor, fColor, fColor, fColor));
        //m_oaSuccessTexts[m_iCurrentText]->setScale(Vector2(fColor, fColor));
        m_oaSuccessTexts[m_iCurrentText]->render();
    }
    else if (m_fTimeSinceLastSuccess < m_fSuccesTextTime * 0.7f)
    {
        float fColor = 1.0f;
        m_oaSuccessTexts[m_iCurrentText]->setColor(Vector4(fColor, fColor, fColor, fColor));        
        //m_oaSuccessTexts[m_iCurrentText]->setScale(Vector2(fColor, fColor));
        m_oaSuccessTexts[m_iCurrentText]->render();
    }
    else if (m_fTimeSinceLastSuccess < m_fSuccesTextTime)
    {
        float fColor = (m_fSuccesTextTime - m_fTimeSinceLastSuccess) / (m_fSuccesTextTime * 0.3f);
        m_oaSuccessTexts[m_iCurrentText]->setColor(Vector4(fColor, fColor, fColor, fColor));        
        //m_oaSuccessTexts[m_iCurrentText]->setScale(Vector2(fColor, fColor));
        m_oaSuccessTexts[m_iCurrentText]->render();
    }
}

void ScoreManager::update(float dt)
{
    m_fTimeSinceLastSuccess += SB::dt;
    
	if(m_bNumeritoStarted && m_eNumeritoAnim->isFinished())
	{
		m_bNumeritoStarted = false;
		m_eCoinsAnim->start(true);
		m_iCoins = m_iCoinsToShow;
		
		char tiChar[5];
		sprintf(tiChar, "%d", m_iCoins);
		m_tCoinsText->setText( tiChar );
	}
    
    m_eChainHolder->setRotation(m_eChainHolder->getRotation() + pow(LevelManager::instance().getGameSpeed(), 4.0) * SB::dt);
    // update bar progress smoothly
    m_fBarProgress = hMath::blend(m_fBarProgress, m_fBarProgressTarget, 60 * SB::dt);
    // update the position of the particles
    if (m_oRainbowEdge)
    {
        Vector2 vPos = m_oRainbowCurve->computeV(1-m_fBarProgress);
        vPos.x = (1-m_fBarProgress) * 640.0f;
        m_oRainbowEdge->position = Vector3(vPos.x, vPos.y, 0.0f);
    }
}

void ScoreManager::render()
{
    //renderSuccessTexts();
    
    if(m_bScoreVisible)
    {
        m_eBabiesIcon->render();
        m_eCoinsIcon->render();
        
        m_tBabiesText->renderShadow();
        m_tBabiesText->render();

        m_tCoinsText->renderShadow();
        m_tCoinsText->render();
        
        m_eNumerito->render();
        
        m_eChainHolder->render();
        m_oChainText->render();
        
        m_tChain->renderShadow(Vector2(2.0f, -2.0f));
        m_tChain->render();
    }
}

void ScoreManager::renderRainbow()
{
    m_eRainbow->setStartUVs(Vector2(1 - m_fBarProgress,0));
    m_eRainbow->render();
}

void ScoreManager::setBabies(int _iBabies)
{
    char tiChar[10];
    sprintf(tiChar, "%d", _iBabies);
    m_tBabiesText->setText( tiChar );
}

void ScoreManager::setCoins(int _iCoins, Vector2 _vPos)
{
	m_iCoinsToShow = _iCoins;
	int _iNumerito = m_iCoinsToShow - m_iCoins;
	
    char tiChar[5];
    sprintf(tiChar, "%d", _iNumerito);
    m_eNumerito->setText( tiChar );
	m_eNumerito->setPosition(_vPos);
    	
	m_eNumeritoAnim->start(true);
	m_bNumeritoStarted = true;
}

void ScoreManager::setChainMultiplier(int _iMultiplier, bool _bPowerUp)
{
    m_iChain = _iMultiplier;
    
    char xChar[4], tiChar[3];
    strcpy(xChar,"X");
    sprintf(tiChar, "%d", _iMultiplier);
    strcat(xChar, tiChar);
    m_tChain->setText( xChar );
	
    if (_iMultiplier == 0)
    {
        m_eChainScaleBig->setParams(Vector2(0.9, 0.9));
        m_eChainScaleSmall->setParams(Vector2(1.0, 1.0));
        if (m_oFullChain) // kill the trail if exists
        {
            m_oFullChain->m_bStopSpawning = true;
            m_oFullChain->m_bRender = false;
        }
    }
    else
    {
        int iScaleMulti = _iMultiplier > 10 ? 10 : _iMultiplier;
		float fValue = 1.0 + (iScaleMulti * 0.03);
        m_eChainScaleBig->setParams(Vector2(fValue + 0.15, fValue + 0.15));
        m_eChainScaleSmall->setParams(Vector2(fValue, fValue));
        m_oAddChainParticles->resetParticles();
        
        //m_iCurrentText = hRandom::getRandomInt(0, SUCCESS_TEXTS - 1);
        //m_oaSuccessTexts[m_iCurrentText]->setRotation(hRandom::getRandom(-0.2f, 0.2f));
        //m_fTimeSinceLastSuccess = 0.0f;
        //m_fSuccesTextTime = 0.5f / LevelManager::instance().getGameSpeed();
    }
	m_eChainScale->start(true);
    
    if (_iMultiplier == 10 || _bPowerUp)
    {
        m_oFullChain->resetParticles();
    }
}

void ScoreManager::showScore(bool _bVisible)
{
    m_bScoreVisible = _bVisible;
}
